/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.sys.service.adv;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import com.thinkgem.jeesite.common.aliyuncs.AliyuncsUtil;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.service.ServiceException;
import com.thinkgem.jeesite.common.utils.ImgUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.hk.sys.entity.adv.HkCityInfo;
import com.thinkgem.jeesite.modules.hk.sys.dao.adv.HkCityInfoDao;
import org.springframework.web.util.UriUtils;

/**
 * 城市信息管理Service
 * @author 罗天文
 * @version 2018-06-05
 */
@Service
@Transactional(readOnly = true)
public class HkCityInfoService extends CrudService<HkCityInfoDao, HkCityInfo> {

	public HkCityInfo get(String id) {
		return super.get(id);
	}
	
	public List<HkCityInfo> findList(HkCityInfo hkCityInfo) {
		return super.findList(hkCityInfo);
	}
	
	public Page<HkCityInfo> findPage(Page<HkCityInfo> page, HkCityInfo hkCityInfo) {
		return super.findPage(page, hkCityInfo);
	}
	
	@Transactional(readOnly = false)
	public void save(HkCityInfo hkCityInfo) {
		String officialCoverImgSrc=hkCityInfo.getOfficialCoverImgSrc();
		if (!officialCoverImgSrc.contains(Global.getFileServer())) {
			officialCoverImgSrc=(Global.getFileServer().trim() + hkCityInfo.getOfficialCoverImgSrc());

		}
		String traditionalCoverImgSrc=hkCityInfo.getTraditionalCoverImgSrc();
		if (!traditionalCoverImgSrc.contains(Global.getFileServer())) {
			traditionalCoverImgSrc=(Global.getFileServer().trim() + hkCityInfo.getTraditionalCoverImgSrc());

		}
		if(StringUtils.isNotEmpty(hkCityInfo.getAtraditionalVideo())) {
			String medisPath = Global.getUserfilesBaseDir()+ hkCityInfo.getAtraditionalVideo();
			try {
				medisPath = UriUtils.decode(medisPath, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				throw new ServiceException("上传失败");
			}
			String videoId = AliyuncsUtil.uploadVideo(hkCityInfo.getCity().getId()+"传统", medisPath, traditionalCoverImgSrc,"" );
			if(StringUtils.isEmpty(videoId)){
				throw new ServiceException("上传失败");
			}
			hkCityInfo.setTraditionalVideo(videoId);
		}
		if(StringUtils.isNotEmpty(hkCityInfo.getAofficialVideo())) {
			String medisPath = Global.getUserfilesBaseDir()+ hkCityInfo.getAofficialVideo();
			try {
				medisPath = UriUtils.decode(medisPath, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				throw new ServiceException("上传失败");
			}
			String videoId = AliyuncsUtil.uploadVideo(hkCityInfo.getCity().getId()+"官方", medisPath, officialCoverImgSrc,"" );
			if(StringUtils.isEmpty(videoId)){
				throw new ServiceException("上传失败");
			}
			hkCityInfo.setOfficialVideo(videoId);
		}
		super.save(hkCityInfo);
	}
	
	@Transactional(readOnly = false)
	public void delete(HkCityInfo hkCityInfo) {
		super.delete(hkCityInfo);
	}
	
}